package com.qf.controller;


import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/dashboard")
public class AdminController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    UserService userService;
    @Autowired
    OrderService orderService;
    @Autowired
    ProductService productService;

    @Autowired
    StateService stateService;


    @RequestMapping("")
    public Object info() {
        Map<String, Long> map = new HashMap<>();
        System.out.println(goodsService.goodsCount());
        System.out.println(userService.userCount());
        System.out.println(productService.selectCount());
        System.out.println(orderService.OrderCount());
        map.put("goodsTotal", goodsService.goodsCount());
        map.put("userTotal", userService.userCount());
        map.put("productTotal", productService.selectCount());
        map.put("orderTotal", orderService.OrderCount());

        return ResponseUtil.ok(map);
    }

    @RequestMapping("/chart")
    public Object chart() {


        List<CategorySellAmts> list = stateService.findCategoryShell();

        String[] categoyrNames = new String[list.size()];
        for (int i = 0; i < categoyrNames.length; i++) {
            categoyrNames[i] = list.get(i).getName();
        }

        CategorySellVo categorySellVo = new CategorySellVo();
        categorySellVo.setCategoryNames(categoyrNames);
        categorySellVo.setCategorySellData(list);
        Map<String, Object> map = new HashMap<>();

//////////////////////////////////////////////////////////////////////////////////////
        List<DayStatis> orderDayStatis = stateService.findOrderDayStatis();

        //提取日期
        List<String> dayData = fetchDayStr(orderDayStatis);
        //提取订单总额
        List<BigDecimal> orderAmt = fechOrderAmt(orderDayStatis, dayData);
        //提取订单的数量
        List<Integer> orderCntData = fetchCntData(orderDayStatis, dayData);
        //封装订单 的数据
        Map<String,Object> orderAmts = new HashMap<>();
        orderAmts.put("dayData", dayData);
        orderAmts.put("orderAmtData", orderAmt);
        orderAmts.put("orderCntData", orderCntData);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
       List<DayStatis> userDay = stateService.findUserDay();

       List<String> dayData1 = fetchDayStr(userDay);

       List<String> dayData2 =unionDate(dayData,dayData1);

       List<BigDecimal> userAmt = fechOrderAmt(userDay,dayData2);
       List<Integer> userCnData = fetchCntData(userDay,dayData2);
       Map map1 = new HashMap();
       map1.put("dayData",dayData2);
       map1.put("userCnt",userAmt);
       map1.put("orderCnt",userCnData);




        ////////////////////////////////////////////////////////////
        map.put("orderAmts",orderAmts);
        map.put("categorySell", categorySellVo);
        map.put("userOrderCnt",map1);

        return ResponseUtil.ok(map);
    }

    private List<String> unionDate(List<String> dayData, List<String> dayData1) {
        Set<String> set = new HashSet<>();
        set.addAll(dayData);
        set.addAll(dayData1);

        List list = new ArrayList();
        list.addAll(set);
        return list;

    }

    private List<String> fetchDayStr(List<DayStatis> orderDayStatis) {
        List list = new ArrayList();
        for(int i=0;i<orderDayStatis.size();i++){
            list.add(orderDayStatis.get(i).getDayStr());
        }
        //对日期集合排序
        Collections.sort(list);

        return list;
    }

    private List<Integer> fetchCntData(List<DayStatis> orderDayStatis, List<String> dayData) {
        List<Integer> list = new ArrayList<>();

        for(int i=0;i<orderDayStatis.size();i++){
            for(int j=0;j<dayData.size();j++){
                if(orderDayStatis.get(i).getDayStr().equalsIgnoreCase(dayData.get(j))){
                    list.add(orderDayStatis.get(i).getCnts());
                }
            }
        }

        return list;
    }

    private List<BigDecimal> fechOrderAmt(List<DayStatis> orderDayStatis, List<String> dayData) {
        List<BigDecimal> list = new ArrayList<>();

        for(int i=0;i<orderDayStatis.size();i++){
            for(int j=0;j<dayData.size();j++){
                if(orderDayStatis.get(i).getDayStr().equalsIgnoreCase(dayData.get(j))){
                    list.add(orderDayStatis.get(i).getAmts());
                }
            }
        }

               return list;
    }


}
